package com.zhike.b2b.company.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import com.zhike.b2b.Query;
import com.zhike.b2b.base.BaseService;
import com.zhike.b2b.company.ICompanyService;
import com.zhike.b2b.model.CompanyLevelVo;
import com.zhike.b2b.model.CompanyVo;

@Service
public class CompanyService  extends BaseService implements ICompanyService {
		/**
		 * 获取新加入的企业列表
		 * @param clzCode 
		 * @param orderStr
		 * @param limitStr
		 * @param commend
		 * @return
		 */
	  public List<CompanyVo> getNewCompanyList(int start,int size)
	  {
	    String str = "select cust_name,cust_supply,cust_id from company where display=1 and cust_state =1 and cust_type!='0' order by add_date desc limit ?,? ";
	    List<CompanyVo> lcv = jt.query(str,new RowMapper<CompanyVo>() {
			 public CompanyVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 CompanyVo cv = new CompanyVo();
				 String custName = rs.getString("cust_name");
				 cv.setCustName(custName);
				 cv.setCustId(rs.getString("cust_id"));
				 cv.setCustSupply(rs.getString("cust_supply"));
				 String url = "/company/web/" +cv.getCustId() + "/templates/index.html";
				 cv.setUrl(url);
				 return cv;
			}},start,size);
		return lcv;
	  }

	/**
	 * 根据条件查询企业列表
	 * @param clzCode 
	 * @param orderStr
	 * @param limitStr
	 * @param commend
	 * @return
	 */
	public List<CompanyVo> queryCompanies(String clzCode,String orderStr, String limitStr, String commend)
	{
		final String defaultLogoImg = ps.getParamValue("default_img");
		List<CompanyVo> lcv = jt.query(getSqlstr(clzCode,orderStr,limitStr,commend),  new RowMapper<CompanyVo>() {
			 public CompanyVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 CompanyVo cv = new CompanyVo();
				 cv.setClassId(rs.getString("class_id"));
				 String custName = rs.getString("cust_name");
				 cv.setCustName(custName);
				 cv.setCustId(rs.getString("cust_id"));
				 String logoImg = rs.getString("logo_img");
				 if(logoImg == null || logoImg.trim().length()<1)
					 logoImg = defaultLogoImg;
				 cv.setLogoImg(logoImg);
				 cv.setOperTime(rs.getString("oper_time"));
				 cv.setAddDate(rs.getString("add_date"));
				 String url = "/company/web/" +cv.getCustId() + "/templates/index.html";
				 cv.setUrl(url);
				 return cv;
			}});
		return lcv;
	}
	
    /**
     * 根据公司的IDs查询公司列表
     * @param custIds
     * @return
     */
	public  List<CompanyVo>  getCompany(String custIds)
	{
		String sql = "select cust_name,cust_id from company where cust_id in ( ? )";
		List<CompanyVo> lcv = jt.query(sql,  new RowMapper<CompanyVo>() {
			 public CompanyVo mapRow(ResultSet rs, int rowNum)
				throws SQLException {
				 CompanyVo cv = new CompanyVo();
				 cv.setClassId(rs.getString("class_id"));
				 String custName = rs.getString("cust_name");
				 cv.setCustName(custName);
				 cv.setCustId(rs.getString("cust_id"));
				 String logoImg = rs.getString("logo_img");
				 if(logoImg == null || logoImg.trim().length()<1)
					 logoImg = defaultLogoImg;
				 cv.setLogoImg(logoImg);
				 cv.setOperTime(rs.getString("oper_time"));
				 cv.setAddDate(rs.getString("add_date"));
				 String url = "/company/web/" +cv.getCustId() + "/templates/index.html";
				 cv.setUrl(url);
				 return cv;
			}},custIds);
		return lcv;
	}
	
	/**
	 * 根据会员公司类别层次查询
	 * @param roleId
	 * @return
	 */
    public List<CompanyLevelVo> getCompanyLevels(String levelName,String isDeleted)
	{
		List<CompanyLevelVo> lu = jt.query(Query.company_level_SEL_COMPANY_LEVEL_TAG,
				new Object[] { levelName+"%", isDeleted}, new RowMapper<CompanyLevelVo>() {
					public CompanyLevelVo mapRow(ResultSet rs, int rowNum)
							throws SQLException {
						CompanyLevelVo clv = new CompanyLevelVo();
						clv.setLevelId(rs.getString("level_id"));
						clv.setLevelName(rs.getString("level_name"));
						clv.setLevelComment(rs.getString("level_comment"));
						clv.setOperTime(rs.getString("oper_time"));
						clv.setOperUser(rs.getString("oper_user"));
						clv.setRemoveTag(rs.getString("remove_tag"));
						clv.setLevelLogo(rs.getString("level_logo"));
						return clv;
					}
				});
		return lu;
	}

    
    private String getSqlstr(String clzCode,String orderStr, String limitStr, String commend)
    {
      StringBuffer sqlBuff = new StringBuffer(" select b.cust_name,b.cust_id,b.class_id,b.logo_img, b.oper_time,b.ADD_date ");
      sqlBuff.append(" from company b where b.display=1 and b.cust_state =1 and b.cust_type = '1' ");
      if ((!clzCode.equals("")) && (!clzCode.equals("100")))
        sqlBuff.append(" and b.cust_supply like '%" + clzCode + "%'");
      else if (!clzCode.equals(""))
        sqlBuff.append(" and b.class_id like '" + clzCode + "%'");
      if (commend.equals("1"))
        sqlBuff.append(" and b.commend=" + commend);
      
      if (orderStr.equals(""))
      {
        sqlBuff.append(" order by b.commend  desc ,b.list_top desc,b.check_time desc  ");
      }else{
    	  sqlBuff.append(" order by b."+ orderStr +" desc  ");
      }
      if (!limitStr.equals(""))
        sqlBuff.append(" limit " + limitStr);
      logger.debug("当前查询的语句为：" + sqlBuff.toString());
      return sqlBuff.toString();
    }
}
